package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.WorkerCertificationMapper;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.dto.WorkerCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.RejectReasonResDTO;
import com.jzo2o.customer.service.IWorkerCertificationService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.regex.Pattern;

/**
 * <p>
 * 服务人员认证信息表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-06
 */
@Service
public class WorkerCertificationServiceImpl extends ServiceImpl<WorkerCertificationMapper, WorkerCertification> implements IWorkerCertificationService {

    @Resource
    private WorkerCertificationMapper workerCertificationMapper;



    /**
     * 根据服务人员id更新
     *
     * @param workerCertificationUpdateDTO 服务人员认证更新模型
     */
    @Override
    public void updateById(WorkerCertificationUpdateDTO workerCertificationUpdateDTO) {
        LambdaUpdateWrapper<WorkerCertification> updateWrapper = Wrappers.<WorkerCertification>lambdaUpdate()
                .eq(WorkerCertification::getId, workerCertificationUpdateDTO.getId())
                .set(WorkerCertification::getCertificationStatus, workerCertificationUpdateDTO.getCertificationStatus())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getName()), WorkerCertification::getName, workerCertificationUpdateDTO.getName())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getIdCardNo()), WorkerCertification::getIdCardNo, workerCertificationUpdateDTO.getIdCardNo())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getFrontImg()), WorkerCertification::getFrontImg, workerCertificationUpdateDTO.getFrontImg())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getBackImg()), WorkerCertification::getBackImg, workerCertificationUpdateDTO.getBackImg())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationMaterial()), WorkerCertification::getCertificationMaterial, workerCertificationUpdateDTO.getCertificationMaterial())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationTime()), WorkerCertification::getCertificationTime, workerCertificationUpdateDTO.getCertificationTime());
        super.update(updateWrapper);

       if (workerCertificationUpdateDTO.getCertificationStatus()==3){
           workerCertificationMapper.updateCertificationStatus(workerCertificationUpdateDTO.getId(),workerCertificationUpdateDTO.getRejectReason());
       }
       if (workerCertificationUpdateDTO.getCertificationStatus()==2){
           workerCertificationMapper.updateCertificationStatus(workerCertificationUpdateDTO.getId(),null);
       }

    }

    @Override
    public PageResult<WorkerCertificationUpdateDTO> pageQuery(WorkerCertificationAuditPageQueryReqDTO workerCertificationAuditPageQueryReqDTO) {
        return PageHelperUtils.selectPage(workerCertificationAuditPageQueryReqDTO,
                () -> baseMapper.queryWorkList(workerCertificationAuditPageQueryReqDTO));

    }



    /**
     * 服务人员实名认证申请
     * @param certiAddReqDTO 服务人员实名认证申请模型
     */

    @Override
    public void apply(WorkerCertificationAuditAddReqDTO certiAddReqDTO) {
        WorkerCertification certification = BeanUtil.toBean(certiAddReqDTO, WorkerCertification.class);
        //1.得到服务人员id,设置id为服务人员id
        Long userId = UserContext.currentUserId();
        //2.设置姓名和身份证校验
        String regexName="^[\u4e00-\u9fa5]{2,15}$";
        String regexIdCardNo="^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$";
        boolean nameMatch = Pattern.matches(regexName, certiAddReqDTO.getName());
        if (!nameMatch){
            throw new ForbiddenOperationException("请输入正确的姓名");
        }
        boolean idCardMatch = Pattern.matches(regexIdCardNo, certification.getIdCardNo());
        if (!idCardMatch){
            throw new ForbiddenOperationException("请输入正确的身份证号");
        }
        certification.setId(userId);
        certification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
        certification.setCertificationTime(LocalDateTime.now());
        super.baseMapper.insert(certification);


        workerCertificationMapper.setAuditStatus(userId);
        workerCertificationMapper.setAuditId(userId);
    }

    @Override
    public RejectReasonResDTO getRejectReason() {
        Long userId = UserContext.currentUserId();
        WorkerCertification certification = super.baseMapper.selectById(userId);
        RejectReasonResDTO reason=workerCertificationMapper.getRejectReason(userId);
        return reason;
    }
}
